require 'carriers/fedex'
require 'carriers/flat'
require 'carriers/free'
require 'carriers/ups'

class ShippingRate
  attr_reader :carriers
  
  def get_rates(zip, weight, carriers=[], options = {})
    @carrier_names = carriers
    
    @carriers = []
    @carrier_names.each do |carrier|
      if not get_class(carrier).nil?
        @carriers << get_class(carrier).new(zip, weight, options)
      end
    end
  end
  
  def get_service_rate(zip, weight, code, options={})
    if not get_class(code[:carrier]).nil?
      @carrier = get_class(code[:carrier]).new(zip, weight,{:code => code[:service_code]})
      return @carrier.rates[0]
    else
      return nil
    end
  end
  
  def parse_service_code(code)
    codearr = code.split('_')
    return nil if codearr.length < 3
    
    split_code = {
      :carrier => codearr[0],
      :service_code => codearr[1],
      :cost => codearr[2]
    }
    
    return split_code
  end
  
  private
  def get_class(name)
    return Object.const_get(name)
  end
  
end